﻿using CleanLite.Shared.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using DbType = SqlSugar.DbType;

namespace CleanLite.Infrastructure.Configuration
{
    public static class SqlsugarConfig
    {
        public static IServiceCollection AddSqlSugarConfiguration(this IServiceCollection services)
        {
            services.AddSingleton<ISqlSugarClient>(s =>
            {
                var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationService.GetString("DB:Connection"));
                var db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = ConfigurationService.GetString("DB:Connection"),
                    DbType = DbType.Sqlite,
                    IsAutoCloseConnection = true
                },
                db =>
                {
                    db.Aop.OnLogExecuting = (sql, pars) =>
                    {
                        Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));
                    };
                });
                return db;
            });

            return services;
        }
    }
}